﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using Assist;

namespace HYFamilyLibDataManager
{
	public class SqlServerFamilyFileTable : IFamilyFileTable
	{
		public SqlServerFamilyFileTable(SqlServerDatabase db)
		{
			this.mDatabase = db;
		}

		public void Update()
		{
		}

		public bool Add(DbFamily family, FileInfo fi, DbTransaction tr)
		{
			byte[] value = Assist.FileUtil.ReadFile(fi.FullName);
			bool result;
			try
			{
				using (SqlCommand sqlCommand = ((SqlConnection)this.mDatabase.Connection).CreateCommand())
				{
					sqlCommand.Transaction = (SqlTransaction)tr;
					sqlCommand.CommandText = string.Format("insert into [FamilyFileTable] ([FamilyGUID],[FamilyFile]) values ('{0}',{1})", family.Guid.ToString(), "@file");
					SqlParameter sqlParameter = new SqlParameter("@file", DbType.Binary);
					sqlParameter.Value = value;
					sqlCommand.Parameters.Add(sqlParameter);
					sqlCommand.ExecuteNonQuery();
				}
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public bool Remove(DbFamily family, DbTransaction tr)
		{
			bool result;
			try
			{
				this.mDatabase.ExecuteCommand("delete from [FamilyFileTable] where [FamilyGUID]='" + family.Guid.ToString() + "'", tr);
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public FileInfo this[DbFamily family]
		{
			get
			{
				string text = Assembly.GetExecutingAssembly().Location;
				int num = text.LastIndexOf("\\");
				text = text.Substring(0, num + 1);
				FileInfo result = null;
				DataTable dataTable = this.mDatabase.ExecQuery("select [FamilyFile] from [FamilyFileTable] where [FamilyGUID]='" + family.Guid.ToString() + "'");
				DataTable dataTable2 = this.mDatabase.ExecQuery("select [FamilyName] from [FamilyTable] where [GUID]='" + family.Guid.ToString() + "'");
				if (dataTable != null && dataTable2 != null)
				{
					byte[] pReadByte = (byte[])dataTable.Rows[0][0];
					string fileName = text + "\\HY_" + dataTable2.Rows[0][0].ToString() + ".rfa";
					Assist.FileUtil.WriteFile(pReadByte, fileName);
					result = new FileInfo(fileName);
				}
				return result;
			}
		}

		public bool Replace(DbFamily family, FileInfo fi)
		{
			DbTransaction dbTransaction = this.mDatabase.StartTransaction();
			byte[] value = Assist.FileUtil.ReadFile(fi.FullName);
			bool result;
			try
			{
				using (SqlCommand sqlCommand = ((SqlConnection)this.mDatabase.Connection).CreateCommand())
				{
					sqlCommand.Transaction = (SqlTransaction)dbTransaction;
					sqlCommand.CommandText = string.Format("update [FamilyFileTable] set [FamilyFile]={0} where [FamilyGUID]='{1}'", "@file", family.Guid.ToString());
					SqlParameter sqlParameter = new SqlParameter("@file", DbType.Binary);
					sqlParameter.Value = value;
					sqlCommand.Parameters.Add(sqlParameter);
					sqlCommand.ExecuteNonQuery();
				}
				dbTransaction.Commit();
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public Dictionary<string, Dictionary<string, byte[]>> GetAllFileInfos()
		{
			Dictionary<string, Dictionary<string, byte[]>> dictionary = new Dictionary<string, Dictionary<string, byte[]>>();
			DataTable dataTable = this.mDatabase.ExecQuery("select [FamilyFile],[FamilyGUID] from [FamilyFileTable]");
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					string text = (string)dataTable.Rows[i][1];
					Dictionary<string, byte[]> dictionary2 = new Dictionary<string, byte[]>();
					byte[] value = (byte[])dataTable.Rows[i][0];
					DataTable dataTable2 = this.mDatabase.ExecQuery("select [FamilyName]  from [FamilyTable]  where GUID='" + text + "'");
					dictionary2.Add((string)dataTable2.Rows[0][0], value);
					dictionary.Add(text, dictionary2);
				}
			}
			return dictionary;
		}

		public bool Replace(string guid, byte[] fileBytes)
		{
			DbTransaction dbTransaction = this.mDatabase.StartTransaction();
			bool result;
			try
			{
				using (SqlCommand sqlCommand = ((SqlConnection)this.mDatabase.Connection).CreateCommand())
				{
					sqlCommand.Transaction = (SqlTransaction)dbTransaction;
					sqlCommand.CommandText = string.Format("update [FamilyFileTable] set [FamilyFile]={0} where [FamilyGUID]='{1}'", "@file", guid);
					SqlParameter sqlParameter = new SqlParameter("@file", DbType.Binary);
					sqlParameter.Value = fileBytes;
					sqlCommand.Parameters.Add(sqlParameter);
					sqlCommand.ExecuteNonQuery();
				}
				dbTransaction.Commit();
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		private SqlServerDatabase mDatabase;
	}
}
